#include <stdio.h>
#include "port.h"
#include "databank.h"
#include "player.h"
#include "interface.h"

Weapon *Interface::nextWeapon(std::vector<Weapon *>wl)
{
    curWeapon++;
    if(unsigned(curWeapon) >= wl.size()) {
        curWeapon = 0;
    }
    return wl[curWeapon];
}

void Interface::render(BITMAP *b, Player *pl, DataBank *d)
{
    tick++;
    if (tick >= 5) {
        glowArc++;
        if (glowArc >= int(pl->getPower() / (400.0))) {
            glowArc = 0;
        }
        tick = 0;
    }

    // Draw power arcs
    // This should look cool! :D
    for (int i = 0; i < int(pl->getPower() / (400.0)); i++) {
        double *p = pl->getPos();
        int pos[2] = {int(p[0]), int(p[1])};
        int ang1 = ftofix(-pl->getDir() + 128.0) - itofix(1.2 * (i + 2));
        int ang2 = ftofix(-pl->getDir() + 128.0) + itofix(1.2 * (i + 2));
        if (ang1 >= -60000 && ang1 <= 0) ang1 = -60000;
        arc(b, pos[0], pos[1], ang1, ang2, 20 + (i + 1) * 3,
                makecol(128 + (i == glowArc) * 127, 0, 0));
    }

    // Draw weapon icons
    std::vector<Weapon *>::iterator wi;
    int j = 0;
    int bitsum = 0;
    for(wi=pl->getWeapons()->begin(); wi != pl->getWeapons()->end(); wi++) {
        BITMAP *pic;
        pic = (*wi)->getPicture((*wi));

        int xpos,ypos;
        xpos = (j+1)*5+bitsum;
        ypos = SCREEN_H - 20 - pic->h;
        if (j==curWeapon) {
            textprintf_centre_ex(b, font, xpos+pic->w/2, ypos+pic->h/2 + 21,
                    makecol(200, 200, 200), -1, "%d", (*wi)->ammo);
            set_trans_blender(255, 255, 255, 255);
            draw_lit_sprite(b, pic, xpos, ypos, 192);
        } else if ((*wi)->ammo <= 0){
            textprintf_centre_ex(b, font, xpos+pic->w/2, ypos+pic->h/2 + 21,
                    makecol(50, 50, 50), -1, "%d", (*wi)->ammo);
            set_trans_blender(48, 48, 48, 255);
            draw_lit_sprite(b, pic, xpos, ypos, 192);
        } else {
            textprintf_centre_ex(b, font, xpos+pic->w/2, ypos+pic->h/2 + 21,
                    makecol(100, 100, 100), -1, "%d", (*wi)->ammo);
            draw_sprite(b, pic, xpos, ypos);
        }

        bitsum += pic->w;
        j++;
    }

    // Draw player info
    textprintf_ex(b, font, SCREEN_W / 2, SCREEN_H - 64 + 12,
            makecol(255,255,255), -1, "Power: %.0f", pl->getPower());
    textprintf_ex(b, font, SCREEN_W / 2, SCREEN_H - 64 + 12 + 11,
            makecol(255,255,255), -1, "Direction: %.0f", pl->getDir());
}
